package com.yunzhong.appointment.system.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.yunzhong.appointment.entity.Department;
import com.yunzhong.appointment.entity.Person;
import com.yunzhong.appointment.entity.Professional;
import com.yunzhong.appointment.entity.SysUser;
import com.yunzhong.appointment.mapper.AppointmentorderMapper;
import com.yunzhong.appointment.mapper.ConstMapper;
import com.yunzhong.appointment.mapper.DepartmentMapper;
import com.yunzhong.appointment.mapper.DoctorIllnessMapper;
import com.yunzhong.appointment.mapper.PersonMapper;
import com.yunzhong.appointment.mapper.ProfessionalMapper;
import com.yunzhong.appointment.mapper.SchedulingMapper;
import com.yunzhong.appointment.mapper.SysRoleMapper;
import com.yunzhong.appointment.mapper.SysUserMapper;
import com.yunzhong.appointment.mapper.VisitsettimeMapper;
import com.yunzhong.appointment.system.service.SysPersonService;
import com.yunzhong.appointment.util.FileUtils;
import com.yunzhong.appointment.util.PageData;

/**
 * @description 人员模型层实现类
 * @author 石洪刚
 * @time 2017年8月15日23:59:30
 */
@Service
public class SysPersonServiceImpl implements SysPersonService{

	@Autowired
	private PersonMapper docMapper;
	@Autowired
	private AppointmentorderMapper orderMapper;
	@Autowired
	private DoctorIllnessMapper docIllMapper;
	@Autowired
	private VisitsettimeMapper visitMapper;
	@Autowired
	private SchedulingMapper scheMapper;
	@Autowired
	private SysUserMapper userMapper;
	@Autowired
	private DepartmentMapper deptMapper;
	@Autowired
	private ProfessionalMapper proMapper;
	@Autowired
	private ConstMapper constMapper;
	@Autowired
	private SysRoleMapper roleMapper;
	
	/**
	 * @description 查询人员信息
	 * @author 石洪刚
	 * @time 2017年9月11日09:34:15
	 * @param pd
	 * @return
	 */
	public List<Person> listPerson(PageData pd) {
		//设置分页数据
		pd.setPageData(pd);
		return docMapper.listPerson(pd);
	}
	
	/**
	 * @description 根据主键查询医生
	 * @author 石洪刚
	 * @time 2017年8月22日00:05:26
	 * @param id
	 * @return
	 */
	public Person getPersonById(String id) {
		Person p = docMapper.selectByPrimaryKey(id);
		//判断当前人员是否为医生
		Set<String> setRoles = roleMapper.listRoleNamesByUserId(p.getUserId());
		if(setRoles!=null && setRoles.contains("doctor")){
			p.setStandby("true");
		}
		return p;
	}
	/**
	 * @description 增加或修改医生
	 * @author 石洪刚
	 * @time 2017年8月22日01:02:06
	 * @param doc
	 * @param picFile
	 * @throws IOException 
	 */
	public void saveOrUpdatePerson(Person doc, MultipartFile picFile) throws IOException {
		String location = FileUtils.saveFile(picFile);
		//如果没选部门，将部门外键设置为null
		if(StringUtils.isBlank(doc.getDepartmentId())){
			doc.setDepartmentId(null);
			doc.setDepartmentName(null);
		}
		//如果没选职称，将职称外键设置为null
		if(StringUtils.isBlank(doc.getProfessionalId())){
			doc.setProfessionalId(null);
			doc.setProfessionalName(null);
			doc.setDoctorPay(null);
		}
		//如果没选用户，将用户外键设置为null
		if(StringUtils.isBlank(doc.getUserId())){
			doc.setUserId(null);
		}else{
			//判断用户是否为医生，如果不是医生就取消出诊费用
			Set<String> roleNames = roleMapper.listRoleNamesByUserId(doc.getUserId());
			if(roleNames==null || !roleNames.contains("doctor")){
				doc.setDoctorPay(null);
			}
		}
		if("".equals(doc.getPpId())){
			doc.setPpId(UUID.randomUUID().toString());
			doc.setPpState("A");
			doc.setPpPic(location);
			
			docMapper.insertSelective(doc);
		}else{
			Person oldDoc = docMapper.selectByPrimaryKey(doc.getPpId());
			if(!picFile.isEmpty()){
				FileUtils.deleteFileByUrl(oldDoc.getPpPic());
				doc.setPpPic(location);
			}
			docMapper.updateByPrimaryKey(doc);
		}
		
	}
	
	/**
	 * @description 删除医生
	 * @author 石洪刚
	 * @time 2017年9月11日09:38:19
	 * @param ids
	 */
	@Transactional
	public void removePerson(String[] ids) {
		for (String id : ids) {
			//将与医生有关的订单外键设置为null
			orderMapper.updateDoctorIdNull(id);
			//删除医生关联疾病中该医生信息
			docIllMapper.removeByDoctorId(id);
			//删除自动排班表中该医生信息
			visitMapper.removeByDoctorId(id);
			//删除手动排班表中该医生信息
			scheMapper.removeByDoctorId(id);
			//删除该人员信息
			docMapper.deleteByPrimaryKey(id);
		}
		
	}
	
	/**
	 * @description 查询未关联用户
	 * @author 石洪刚
	 * @time 2017年9月12日10:47:45
	 * @return
	 */
	public List<SysUser> listUserNotLinked() {
		
		return userMapper.listUserNotLinked();
	}
	
	/**
	 * @description 查询科室信息
	 * @author 石洪刚
	 * @time 2017年9月12日10:48:19
	 * @return
	 */
	public List<Department> listDepartment() {
		
		return deptMapper.listDepartment(null);
	}

	/**
	 * @description 根据医生主键查询被关联用户
	 * @param id
	 * @return
	 */
	public SysUser getLinkedUserByPpId(String id) {
		
		return userMapper.getLinkedUserByPpId(id);
	}

	/**
	 * @description 根据部门主键查询职务
	 * @author 石洪刚
	 * @time 2017年9月12日11:32:45
	 * @param departmentId
	 * @return
	 */
	public List<Professional> listProfessionalByDeptId(String departmentId) {
		return proMapper.listProfessionalByDeptId(departmentId);
	}

	@Override
	public void checkRoleDoc(List<SysUser> userList) {
		for (SysUser user : userList) {
			Set<String> setRoles = roleMapper.listRoleNamesByUserId(user.getUserId());
			if(setRoles!=null && setRoles.contains("doctor")){
				user.setHasDocRole(true);
			}
		}
	}
	
}
